package aam.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import aam.model.PagingBean;
import aam.model.SubBoardListVO;
import aam.model.subboard.SubBoardDao;
import aam.model.subboard.SubBoardVO;

@Service
public class SubBoardServiceImpl implements SubBoardService {
	@Resource(name="subBoardDaoImpl")
	private SubBoardDao subBoardDao;
	//방명록 list -dw
		@Override
		public SubBoardListVO getAllGuestBookList(String pageNo) throws Exception {
			int p=1;
			if(pageNo!=null){
				p=Integer.parseInt(pageNo);
			}
			ArrayList<SubBoardVO> list=(ArrayList<SubBoardVO>) this.subBoardDao.getAllGuestBookList(p);
			PagingBean pb=new PagingBean(p,this.subBoardDao.getBoardTotalConunt());
			return new SubBoardListVO(list,pb);
		}
		//방명록 글쓰기 -dw
		@Override
		public int writeGuestBook(String id, String content) throws Exception {
			HashMap map=new HashMap();
			map.put("id", id);
			map.put("content", content);
			return this.subBoardDao.writeGuestBook(map);
		}
		//방명록 삭제 -dw
		@Override
		public int deleteGuestBook(String no) throws Exception {
			return this.subBoardDao.deleteGuestBook(Integer.parseInt(no));
		}
		
		//댓글쓰기
		@Override
		public void writeReply(SubBoardVO vo) throws Exception {
			subBoardDao.writeReply(vo);
			subBoardDao.updatePointWriteReply(vo.getMemberVO().getId());
		}
			//댓글가져오기
		@Override
		public List<SubBoardVO> getReplyByNo(int no) throws Exception {
			return subBoardDao.getReplyByNo(no);
		}
		@Override
		public void deleteReply(int replyNo) throws Exception {
			subBoardDao.deleteReply(replyNo);
		}
		
		@Override
		public SubBoardListVO replyPaging(String page,int no) throws Exception {
			int p=1;
			HashMap map=new HashMap();
			map.put("postNo", no);
			if(page!=null){
			p=Integer.parseInt(page);
			}
			map.put("pageNo",p);
			ArrayList<SubBoardVO> list=(ArrayList<SubBoardVO>) subBoardDao.replyPaging(map);
			
			PagingBean pb=new PagingBean(p,subBoardDao.getReplyTotalConunt(no));
			return new SubBoardListVO(list,pb);
		}
		@Override
		 public void saveReport(String no) throws Exception {	
					int result=subBoardDao.updateReport(no);
					if(result==0)
						subBoardDao.insertReport(no);			
			}	
		@Override
		public List selectReport() throws Exception {
				return subBoardDao.selectReport();
			}
	
}
